import Vue from 'vue'
import Vuex from 'vuex'
import api from "@/api/api"
import MinCache from'@/common/util/MinCache.js'
import { ACCESS_TOKEN,REFRESH_TOKEN,EXPIRES_IN_TOKEN,SCOPE_TOKEN,TOKEN_TYPE,USER_NAME,USER_INFO } from "@/common/util/constants"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
	refreshToken:'',
	expiresInToken:0,
	scopeToken:'',	
	tokenType:'',
    userid:'',
    username: '',
    realname: '',
    welcome: '',
    avatar: ''
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
	SET_REFRESH_TOKEN: (state, refreshToken) => {
	  state.refreshToken = refreshToken
	},
	SET_EXPIRES_IN_TOKEN: (state, expiresInToken) => {
	  state.expiresInToken = expiresInToken
	},
	SET_SCOPE_TOKEN: (state, scopeToken) => {
	  state.scopeToken = scopeToken
	},
	SET_TOKEN_TYPE: (state, tokenType) => {
	  state.tokenType = tokenType
	},
	
    SET_NAME: (state, { username, realname, welcome }) => {
      state.username = username
      state.realname = realname
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    }
  },
  actions: {
    // 登录
    mLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
       api.login(userInfo).then(response => {
          if(response.data.code ==200){ 
            const result = response.data.content.tokenInfo
			uni.setStorageSync(ACCESS_TOKEN,result.access_token);
			uni.setStorageSync(REFRESH_TOKEN,result.refresh_token);
			uni.setStorageSync(EXPIRES_IN_TOKEN,result.expires_in);
			uni.setStorageSync(SCOPE_TOKEN,result.scope);
			uni.setStorageSync(TOKEN_TYPE,result.token_type);
            commit('SET_TOKEN', result.access_token);
			commit('SET_REFRESH_TOKEN', result.refresh_token);
			commit('SET_EXPIRES_IN_TOKEN', result.expires_in);
			commit('SET_SCOPE_TOKEN', result.scope);
			commit('SET_TOKEN_TYPE', result.token_type);
			
			api.me().then(response2=> {
				const userInfo = response2.data.content.userInfo
				uni.setStorageSync(USER_INFO,userInfo);
				commit('SET_AVATAR', userInfo.avatar)
				commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})				
				resolve()
			}).catch(() => {
				resolve()
			})
			
            resolve(response)
          }else{
            resolve(response)
          }
        }).catch(error => {
			console.log('error',error)
			// console.log("catch===>response",response)
			reject(error)
        })
      })
    },
    //手机号登录
    PhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.phoneNoLogin(userInfo).then(response => {
          if(response.data.code ==200){
            const result = response.data.result
            const userInfo = result.userInfo
			uni.setStorageSync(ACCESS_TOKEN,result.token);
			uni.setStorageSync(USER_INFO,userInfo);
            commit('SET_TOKEN', result.token)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
            commit('SET_AVATAR', userInfo.avatar)
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
		uni.removeStorageSync(ACCESS_TOKEN);
		uni.removeStorageSync(REFRESH_TOKEN);
		uni.removeStorageSync(EXPIRES_IN_TOKEN);
		uni.removeStorageSync(SCOPE_TOKEN);
		uni.removeStorageSync(TOKEN_TYPE);
		commit('SET_TOKEN', '')
		commit('SET_REFRESH_TOKEN', '')
		commit('SET_EXPIRES_IN_TOKEN', 0)
		commit('SET_SCOPE_TOKEN', '')
		commit('SET_TOKEN_TYPE', '')
		uni.removeStorageSync(USER_INFO);
		commit('SET_AVATAR', '')
		commit('SET_NAME', { username: '',realname: ''})
        api.logout(logoutToken).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    },
  },

  getters:{
    token: state => state.token,
	
	refreshToken: state => state.refreshToken,
	expiresInToken: state => state.expiresInToken,
	scopeToken: state => state.scopeToken,
	tokenType: state => state.tokenType,
	
	username: state => {state.userid=uni.getStorageSync(USER_INFO).username; return state.username},
	nickname: state => {state.userid=uni.getStorageSync(USER_INFO).realname; return state.user.realname},
	avatar: state => {state.userid=uni.getStorageSync(USER_INFO).avatar; return state.user.avatar},
	userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
  }
})
